﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using SXC.Control;
namespace LED.DataBase
{
    /// <summary>
    /// Interaction logic for VIEWLEDDataBase.xaml
    /// </summary>
    public partial class VIEWLEDDataBase : UserControl
    {
        LEDData m_led=new LEDData();
        public VIEWLEDDataBase()
        {
            InitializeComponent();
        }
        LEDDataSet setUI
        {
            get
            {
                return (LEDDataSet)this.FindResource("lEDDataSet");
            }
        }
        CollectionViewSource vwLEDLIST
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLEDLIST");
            }
        }
        CollectionViewSource vwLEDMATER
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLEDMATER");
            }
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            // 	//Load your data here and assign the result to the CollectionViewSource.
            // 	System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            // 	myCollectionViewSource.Source = your data
            // }
        }

        private void cmdLoadDataBase_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setUI.Clear();
                DataSet dsResult = new DataSet();
                SqlCommand sql = new SqlCommand();
                string sqlConnString = "Data Source=SUNLIGHT;Persist Security Info=True;Password=LanSunERP2010;User ID=LanSunERP2010;Initial Catalog=LEDMANAGEMENT";
                sql.CommandType = CommandType.Text;
                sql.Connection = new SqlConnection(sqlConnString);
                string CommandText = "SELECT * FROM LEDMANAGEMENT.dbo.";
                SqlDataAdapter mySqlDa = new SqlDataAdapter(sql);
                ArrayList cmdList = new ArrayList(); 
                foreach (DataTable db in setUI.Tables)
                {
                    cmdList.Add(db.TableName);
                }
                foreach(string tbName in cmdList)
                {
                    CommandText = "SELECT * FROM LEDMANAGEMENT.dbo."+tbName;
                    sql.CommandText = CommandText;
                    mySqlDa = new SqlDataAdapter(sql);
                    mySqlDa.Fill(dsResult);
                    if (dsResult.Tables.Count > 0)
                    {
                        dsResult.Tables[0].TableName = tbName;
                    }
                    setUI.Merge(dsResult);
                    dsResult = new DataSet(); 
                }
            }
            catch (Exception ex)
            {
                //Tracer.Error(ex.StackTrace, ex);
            }
        } 
        private void cmdSave2Local_Click(object sender, RoutedEventArgs e)
        { 
            try
            {
                m_led.SaveLocalData(setUI);
            }
            catch (Exception ex)
            {
                //Tracer.Error(ex.StackTrace, ex);
            } 
        }

        private void cmdReadLocal_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setUI.Clear(); 
                setUI.Merge(m_led.LoadLocalData());
            }
            catch (Exception ex)
            {
                //Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
